$(document).ready(function(){
    if(window.location.pathname=='/newsDetail.html'||window.location.pathname=='/policyDetail.html'){
        var title=localStorage.getItem("title");
        var time=localStorage.getItem("time");
        var content=localStorage.getItem("content");
        $('#title').text(title);
        $('#time').text(time);
        $('#content').text(content);
    }else{
        var data=[];
        var aList=[];
        var pageNews=$(".page-news");
        var newsList=$(".news-list");
        var pageNum=parseInt($("#page-num").text());
        var total=0;
        var clickNews=function(){
            for(var i=0;i<aList.length;i++){
                aList[i].onclick=function (e) {
                    var index=e.target.id;
                    localStorage.setItem("content",data[index].policyDesc);
                    localStorage.setItem("title",data[index].policyName);
                    localStorage.setItem("time",data[index].releaseTime.slice(0,10));
                    var href=window.location.hostname;
                    console.log(href)
                    window.location.href="http://"+href+":8888/policyDetail.html";
                }
            }
        }
        $.ajax({
            type: "POST",
            url: 'http://localhost:8888/government/getPolicies',
            data: {
                pageNum:pageNum,
                pageSize:10
            },
            success: function (result) {
                if(result.meta.success){
                    data=result.data["governmentPolicies List"];
                    var data1=data.map(function (item,index) {
                        return '<li class="news_item"><span class="new_link"><a href="javascript:void(0)" id="'+index+'">'+item.policyName+'</a></span><span class="new_date">'+item.releaseTime.slice(0,10)+'</span></li>';
                    });
                    newsList.remove();
                    pageNews.append('<ul class="news-list"></ul>');
                    var newsUl=$('.news-list');
                    newsUl.append(data1);
                    total=result.data['Total Page Num'];
                    $('#total-page').text('共'+total+'页');
                    aList=$('.new_link');
                    clickNews()
                }else{
                    alert(result.meta.message);
                }
            },
            error:function(){
                alert("网络错误");
            }
        });

        $(".page-next").click(function () {
            var pageNum=parseInt($("#page-num").text());
            if(pageNum<total){
                $("#page-num").html(pageNum+1);
                $(".news-list").remove();
                $.ajax({
                    type: "POST",
                    url: 'http://localhost:8888/government/getPolicies',
                    data: {
                        pageNum:pageNum+1,
                        pageSize:10
                    },
                    success: function (result) {
                        if(result.meta.success){
                            data=result.data["governmentPolicies List"];
                            var data1=data.map(function (item,index) {
                                return '<li class="news_item"><span class="new_link"><a href="javascript:void(0)" id="'+index+'">'+item.policyName+'</a></span><span class="new_date">'+item.releaseTime.slice(0,10)+'</span></li>'
                            });
                            pageNews.append('<ul class="news-list"></ul>');
                            var newsUl=$('.news-list');
                            newsUl.append(data1);
                            total=result.data['Total Page Num'];
                            $('#total-page').text('共'+total+'页');
                            aList=$('.news-list a');
                        }else{
                            alert(result.meta.message);
                        }
                    },
                    error:function(){
                        alert("网络错误");
                    }
                });
            }
        })
        $(".page-prev").click(function () {
            var pageNum=parseInt($("#page-num").text());
            if(pageNum>1){
                $("#page-num").html(pageNum-1);
                $(".news-list").remove();
                $.ajax({
                    type: "POST",
                    url: 'http://localhost:8888/government/getPolicies',
                    data: {
                        pageNum:pageNum-1,
                        pageSize:10
                    },
                    success: function (result) {
                        if(result.meta.success){
                            data=result.data["governmentPolicies List"];
                            var data1=data.map(function (item,index) {
                                return '<li class="news_item"><span class="new_link"><a href="javascript:void(0)" id="'+index+'">'+item.policyName+'</a></span><span class="new_date">'+item.releaseTime.slice(0,10)+'</span></li>';
                            });
                            pageNews.append('<ul class="news-list"></ul>');
                            var newsUl=$('.news-list');
                            newsUl.append(data1);
                            total=result.data['Total Page Num'];
                            $('#total-page').text('共'+total+'页');
                            aList=$('.news-list a');
                        }else{
                            alert(result.meta.message);
                        }
                    },
                    error:function(){
                        alert("网络错误");
                    }
                });
            }
        })
    }

});